package com.stud.seckill.common.exception;

import com.alibaba.fastjson.JSON;
import com.stud.seckill.common.vo.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(GlobalException.class)
    public R<?> exceptionHandler(GlobalException e) {
        if (e.getResultCode() != null) {
            ResultCode resultCode = e.getResultCode();
            log.info("GlobalException全局异常处理 结果码：{}", JSON.toJSONString(resultCode));
            return R.fail(resultCode.getCode(), resultCode.getMessage());
        }
        log.info("GlobalException全局异常处理 消息：{}", e.getMessage());
        return R.fail(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public R<?> exceptionHandler(Exception e) {
        log.info("Exception全局异常处理 消息：{}", e.getMessage());
        return R.fail(e.getMessage());
    }

}
